home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DOS.SWG / 0053_No DOS Shell.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  8KB  |  284 lines

  1. {
  2. Ever been in a situation where you want to secure a PC (for example in a
  3. network environment) by using menus from which you can't exit and
  4. user/software companies keep coming with software with the Shell to DOS
  5. option?
  6.  
  7. Here's a simple solution which works with a lot of programs which shell
  8. by using COMSPEC.
  9.  
  10. This program called execute patches it's own environment with a
  11. replacement COMSPEC, Does an EXEC and restores the original environment.
  12. It's done by making fetching all environment strings, replace comspec
  13. with the first commandline parameter (which should be shorter than the
  14. original comspec, so I use the program called EXIT located in the
  15. same directory as COMMAND.COM). Than it does an plain TP Exec (without
  16. swapping to EMS/XMS/DISK etc) of the second commandline parameter with
  17. the rest of the commandline as it's parameters.
  18.  
  19. I used patching the original environment of EXECUTE because the program
  20. executed inherits it and EXECUTE needs comspec only to exit itself (and
  21. return to a menu for example). Because of this construction it's
  22. possible to exit the program started normally and return to a menu but
  23. you'll be unable to shell to dos and type something like FORMAT C:.
  24.  
  25. An example EXIT.PAS is also supplied. Pressing CTRL-BREAK etc doesn't
  26. matter, you'll always return to the application from which you tried to
  27. shell. Beware that some programs like SPSS and VP-Planner have
  28. difficulties with R/O attributes on EXIT.EXE (and COMMAND.COM), so keep
  29. it R/W.
  30.  
  31. So to for example disable the Turbo Pascal File/Dos use :
  32.  
  33. EXECUTE C:\DOS\EXIT.EXE C:\TURBO55\TURBO.EXE TEST.PAS
  34.  
  35. instead of
  36.  
  37. C:\TURBO55\TURBO TEST.PAS
  38.  
  39. If COMSPEC was C:\DOS\COMMAND.COM and Turbo Pascal was located in
  40. the C:\TURBO55 directory.
  41.  
  42.  
  43. Remember the extensions .EXE or .COM are necessary!
  44.  
  45. ------------------------<cut here
  46.  
  47. {---------------------------------------------------------}
  48. {  Project : Exec with Temporaryly changed 'COMSPEC'      }
  49. {          : the exec routine itself                      }
  50. {  Auteur  : Ir. G.W. van der Vegt                        }
  51. {---------------------------------------------------------}
  52. {  Datum .tijd  Revisie                                   }
  53. {  921118.0930  Creatie.                                  }
  54. {---------------------------------------------------------}
  55. { This program patches the COMSPEC environment variable   }
  56. { with a new value (ie EXIT.EXE) and executes the         }
  57. { program. After execution it restores the environment    }
  58. {                                                         }
  59. { Syntax :                                                }
  60. {                                                         }
  61. { EXECUTE temporary_comspec program_name [paramaters]     }
  62. {                                                         }
  63. { Limits :-Only maxenv environments strings can be stored,}
  64. {          each with a maximum length of 128 characters.  }
  65. {         -The temporary comspec must be shorter than the }
  66. {          original one.                                  }
  67. {         -Environment must be smaller than 32k           }
  68. {---------------------------------------------------------}
  69.  
  70. {$M 4096,0,0}
  71.  
  72. Program Execute;
  73.  
  74. Uses
  75.   Crt,
  76.   Dos;
  77.  
  78.  
  79. Const
  80.   Maxenv = 64;
  81.  
  82. Type
  83.   psp = Record
  84.           int20adr : Word;
  85.           endofmem : Word;
  86.           res1     : Byte;
  87.           callfar  : Array[1..5] OF Byte;
  88.           int22    : Pointer;
  89.           Int23    : Pointer;
  90.           Int24    : Pointer;
  91.           parentpsp: Word;
  92.           handles  : Array[1..20] OF Byte;
  93.           envseg   : Word;
  94.         {----More follows}
  95.         End;
  96.  
  97.   env = array[1..32678] OF Char;
  98.  
  99. Var
  100.   e      : ^env;
  101.   p      : ^psp;
  102.   addcnt : Word;                           {----no of additional strings}
  103.   i      : Integer;                        {----loop counter}
  104.   envar  : Array[1..maxenv] of String[128];{----environment string storage}
  105.   noenv  : Integer;                        {----no strings in environment}
  106.   cmdline: STRING;                         {----command line of program to start}
  107.   comspec: STRING;                         {----original comspec storage}
  108.   ch     : CHAR;
  109.  
  110. {---------------------------------------------------------}
  111.  
  112. Procedure Read_env;
  113.  
  114. Var
  115.   i,k : Integer;
  116.  
  117. begin
  118.   p:=Ptr(prefixseg,0);
  119.   noenv:=0;
  120.  
  121. {----Show environment strings}
  122.   e:=Ptr(p^.envseg,0);
  123.   i:=1;
  124.   Inc(noenv);
  125.   envar[noenv]:='';
  126.   Repeat
  127.     If (e^[i]<>#0)
  128.       Then envar[noenv]:=envar[noenv]+e^[i]
  129.       Else
  130.         Begin
  131.           Inc(noenv);
  132.           If (noenv>=maxenv)
  133.             THEN
  134.               BEGIN
  135.                 Writeln('Only ',maxenv:0,' environment strings can be stored.');
  136.                 Halt;
  137.               END;
  138.  
  139.           envar[noenv]:='';
  140.         End;
  141.     Inc(i);
  142.   Until (e^[i]=#00) AND (e^[i]=e^[i-1]);
  143.  
  144. {----Show Additional environment strings}
  145.   Inc(i);
  146.   addcnt:=Word(Ord(e^[i])+256*Ord(e^[i+1]));
  147.   Inc(i);
  148.   Inc(i); {----eerste character additional strings}
  149.   k:=addcnt;
  150.  
  151.   If (noenv+addcnt>=maxenv)
  152.     THEN
  153.       BEGIN
  154.         Writeln('Only ',maxenv:0,' (additional)environment strings can be stored');
  155.         Halt;
  156.       END;
  157.  
  158.   Repeat
  159.     If (e^[i]<>#0)
  160.       Then envar[noenv]:=envar[noenv]+e^[i]
  161.       Else
  162.         Begin
  163.           Inc(noenv);
  164.           envar[noenv]:='';
  165.           Dec(k);
  166.         End;
  167.     Inc(i);
  168.   Until (k<=0);
  169.  
  170.   dec(noenv);
  171.  
  172.  {Writeln(' Environment Strings : ',noenv-addcnt);
  173.   for j:=1 to noenv-addcnt do
  174.     writeln('e ',envar[j]);
  175.   Writeln(' Additional Strings : ',addcnt);
  176.   for j:=noenv-addcnt+1 to noenv do
  177.     writeln('a ',envar[j]);
  178.   writeln;}
  179. end; {of Read_env}
  180.  
  181. {---------------------------------------------------------}
  182.  
  183. Procedure Patch_env(envst,newval : STRING);
  184.  
  185. Var
  186.   i,j,k : Integer;
  187.  
  188. BEGIN
  189. {----change an envronment string}
  190.   for i:=1 to noenv do
  191.     begin
  192.       if (pos(envst+'=',envar[i])=1)
  193.         THEN
  194.           begin
  195.             Delete(envar[i],Pos('=',envar[i])+1,Length(envar[i])-Pos('=',envar[i]));
  196.             envar[i]:=envar[i]+newval;
  197.           end;
  198.     end;
  199.  
  200. {----patch environment strings}
  201.   i:=1;
  202.   for j:=1 to noenv-addcnt do
  203.     begin
  204.       for k:=1 to Length(envar[j]) do
  205.         begin
  206.           e^[i]:=envar[j][k];
  207.           inc(i);
  208.         end;
  209.       e^[i]:=#0;
  210.       inc(i);
  211.     end;
  212.  
  213. {----patch environment string end}
  214.   e^[i]:=#0;                  inc(i);
  215. {----patch additional string count}
  216.   e^[i]:=Chr(addcnt mod 256); inc(i);
  217.   e^[i]:=Chr(addcnt div 256); inc(i);
  218.  
  219. {----patch additional strings}
  220.   for j:=noenv-addcnt+1 to noenv do
  221.     begin
  222.       for k:=1 to Length(envar[j]) do
  223.         begin
  224.           e^[i]:=envar[j][k];
  225.           inc(i);
  226.         end;
  227.       e^[i]:=#0;
  228.       inc(i);
  229.     end;
  230. end; {of Patch_env}
  231.  
  232. {---------------------------------------------------------}
  233.  
  234. Begin
  235.   If (Paramcount<2)
  236.     THEN
  237.       BEGIN
  238.         Writeln('Syntax : EXECUTE temporary_comspec program_name [program_param]');
  239.         Halt;
  240.       END;
  241.  
  242.   checkbreak:=false;
  243.  
  244.   comspec:=Getenv('COMSPEC');
  245.  
  246.   If (Length(Paramstr(1))>Length(comspec))
  247.     THEN
  248.       BEGIN
  249.         Writeln('Path&name of temporary COMSPEC should be shorter than the original');
  250.         Halt;
  251.       END;
  252.  
  253.   Read_env;
  254.  
  255.   Patch_env('COMSPEC',Paramstr(1));
  256.  
  257.   cmdline:='';
  258.   FOR i:=3 to Paramcount DO
  259.     cmdline:=cmdline+' '+Paramstr(i);
  260.  
  261.   Swapvectors;
  262.   Exec(Paramstr(2),cmdline);
  263.   Swapvectors;
  264.  
  265.   WHILE Keypressed DO ch:=Readkey;
  266.  
  267.   Patch_env('COMSPEC','C:\COMMAND.COM');
  268. end.
  269.  
  270.  
  271. ------------------------<cut here
  272.  
  273.  
  274. Program Exit;
  275.  
  276. Uses
  277.   CRT;
  278.  
  279. Begin
  280.  Clrscr;
  281.  GotoXY(20,12);
  282.  Write('Sorry, SHELLing to DOS not Possible.');
  283. End.
  284.